package com.lsmo.android.carfans.activity.car;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.lsmo.android.carfans.R;
import com.lsmo.android.carfans.app.widget.RemoteImageView;
import com.lsmo.android.carfans.model.car.BrandTypeThread;
import com.lsmo.android.carfans.model.car.CarTypeThread;
import com.lsmo.android.carfans.process.car.BrandTypeProcess;
import com.lsmo.android.carfans.view.car.SelectCarView;
import com.lsmo.android.common.base.BaseActivity;
import com.lsmo.android.common.base.BaseProcess;
import com.lsmo.android.common.base.BaseTask;
import com.lsmo.android.common.base.ViewProcessCallBack;

public class CarTypeActivity extends BaseActivity 
	implements AdapterView.OnItemClickListener, View.OnClickListener,ViewProcessCallBack{

	public static final String TAG = SelectCarView.class.getSimpleName();
	
	private ArrayList<CarTypeThread> brandCarType;
	private ArrayList<BrandTypeThread> brandTypeThreads;
	
	private View localView;
	private ListView mListView;
	public Context mContext ;
	private ViewGroup mProgressBarLayout;
	private BrandTypeProcess bTypeProcess;
	private String[] params = null;
	public static int ids,positions;
	public static String names,carNames;
	
	private RemoteImageView carImage;
	CarTypeThread carType;
	private TextView changjzdjia,daochld,fjsanqn,hzdlx,
		jingxiaosbj,lunkcl,pailiang,qzdlx,sjyh,sqgkyh,
		tianchxs,titleText,zhyh,zuidagl,zuidaniuj,zuixiaoldjx,biansux;
	private String url;
	
	
	public static void launch(Context c,int position,int id,String name,String carName){
		Intent intent = new Intent(c, CarTypeActivity.class);
		ids = id;
		positions = position;
		names = name;
		carNames = carName;
		c.startActivity(intent);
	}
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.bTypeProcess = new BrandTypeProcess();
		this.mContext = CarTypeActivity.this;
		
		initView();//加载view
		
		updateView();//加载页面数据
	}
	
	//加载view
	public void initView(){
		setContentView(R.layout.car_view_cartype);
		mListView = (ListView)this.findViewById(R.id.listView);
		mListView.setClickable(true);
		mListView.setOnItemClickListener(this);//注册listItem监听
 		mProgressBarLayout = (ViewGroup)this.findViewById(R.id.loading_layout);
 		
 		TextView titleView = (TextView)this.findViewById(R.id.title_bar_layout_tv_id);
 		titleView.setGravity(Gravity.CENTER);
 		titleView.setText(names);
 		
 		//设置listview，加载listviewUI
 		mListView.setHeaderDividersEnabled(true);
 		localView = getLayoutInflater().inflate(R.layout.car_component_cartype, null);
 		localView.setOnClickListener(this);
 		
 		this.titleText = (TextView)localView.findViewById(R.id.brandname);//车类型
 		this.jingxiaosbj = (TextView)localView.findViewById(R.id.jingxiaosbj);//经销商报价
 		this.changjzdjia = (TextView)localView.findViewById(R.id.changjzdjia);//厂家指导价
 		this.pailiang = (TextView)localView.findViewById(R.id.pailiang);//排量
 		this.carImage = (RemoteImageView)localView.findViewById(R.id.Car_Image);//车图片
 		this.biansux = (TextView)localView.findViewById(R.id.biansux);//变速箱
 		this.zhyh = (TextView)localView.findViewById(R.id.zhyh);//综合工况油耗
 		this.sqgkyh = (TextView)localView.findViewById(R.id.sqgkyh);//市区工况油耗
 		this.sjyh = (TextView)localView.findViewById(R.id.sjyh);//市郊工况油耗
 		this.qzdlx = (TextView)localView.findViewById(R.id.qzdlx);//前置动类型
 		this.hzdlx = (TextView)localView.findViewById(R.id.hzdlx);//后置动类型 
 		this.fjsanqn = (TextView)localView.findViewById(R.id.Fujiasanqn);//副驾驶安全气囊
 		
 		this.zuidagl = (TextView)localView.findViewById(R.id.zuidagl);//最大功率
 		this.zuidaniuj = (TextView)localView.findViewById(R.id.zuidaniuj);//最大扭矩
 		this.zuixiaoldjx = (TextView)localView.findViewById(R.id.zuixiaoldjx);//最小距离间距
 		this.lunkcl = (TextView)localView.findViewById(R.id.lunkcl);//轮毂材料
 		this.tianchxs = (TextView)localView.findViewById(R.id.tianchxs);//天窗型式
 		this.daochld = (TextView)localView.findViewById(R.id.daochld);//倒车雷达
 		
 		mListView.addHeaderView(localView);
 		
	}
	
	/**
	 * 刷新页面数据
	 */
	public void refreshView(){
		//加载图片
		InputStream localInputStream;
    	AssetManager localAssetManager = this.mContext.getResources().getAssets();
    	String str1 = brandCarType.get(0).getCarImg();
		try {
			
			localInputStream = localAssetManager.open(str1);
			Drawable localDrawable = Drawable.createFromStream(localInputStream, str1);
			this.carImage.setBackgroundDrawable(localDrawable);
		    
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	    
	}


	/**
	 * listview 监听事件
	 * @param method
	 * @param param
	 */
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
		
	}

	/**
	 * 按钮监听
	 */
	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void processAfterTaskFinish(int code, Object result) {
		// TODO Auto-generated method stub
		if(result !=null){
			refreshView();
		}
		
		this.mProgressBarLayout.setVisibility(View.GONE);
		this.mListView.setVisibility(View.VISIBLE);
	}

	@Override
	public void updateView() {
		// TODO Auto-generated method stub
		
		this.mProgressBarLayout.setVisibility(View.VISIBLE);
		this.mListView.setVisibility(View.GONE);
		
			new CarTypeTask(CarTypeTask.GET_CAR_TYPE_LIST,
				this.mContext,
				this,
				this.bTypeProcess 
		).execute(new String[]{});
	}
	
	private class CarTypeTask extends BaseTask{
		
		public static final int GET_CAR_TYPE_LIST = 10;
		private BrandTypeProcess bTypeProcess;
		
		public CarTypeTask(int code, Context context, ViewProcessCallBack view,BaseProcess process) {
			super(code, context, view);
			// TODO Auto-generated constructor stub
			this.bTypeProcess = (BrandTypeProcess)process;
		}
		
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
		}
		
		@Override
		protected Object doInBackground(String... params) {
			super.doInBackground(params);
			Object result = null;
	 
			switch(this.mCode){
			    
				case GET_CAR_TYPE_LIST:
					
					//加载数据
					result = bTypeProcess.getCarTypeThreadList(params, ids);
					brandCarType = (ArrayList<CarTypeThread>) result;
					
					brandTypeThreads = (ArrayList<BrandTypeThread>) bTypeProcess.getBrandTypeThreadList(params, ids);
					
					break;
			}
			return result;
		}
		
		@Override
		protected void onPostExecute(Object result) {
			super.onPostExecute(result);
			switch(this.mCode){
				 
				case GET_CAR_TYPE_LIST:
					
				 		mView.processAfterTaskFinish(this.mCode ,result );
				 	
				 	break;
		   }
		}
		
	}

}
